<?php
/*DEE_DESC
 $templ=array(
	//Название шаблона в списке ОБЯЗАТЕЛЬНОЕ
	'title'=>'Страница пользователя'
	);
//DEE_DESC_END*/

	if( !$user->id )
		header("Location: http://".$_SERVER['HTTP_HOST']);
			
	$view->meta_robots = 'noindex,follow';
	
	$adresses = $user->getData('adress');

	$conn->query("SELECT *, date_format(date_order,'%d.%m.%Y  %H:%i:%s') AS date_order_f FROM store_baskets WHERE status !='basket' AND user_id = {$user->id} ORDER BY date_order DESC ");
	$statuses = array( 'order' => 'Активный', 'done' => 'Выполнено', 'cencel' => 'Отменён'	);
	
	if( $conn->rowCount() )
	{
		$orders = '';
		while( $order = $conn->fetch() )
		{
			$total = 0;
			$products = '';
			$courier_price = ( $order->delivery_type == 'courier' ) ? $configs->getVal('store','courier_price') : 0;

			$product_list = getProductList($conn,'store_order_view',"basket_id={$order->id}",array('available_count'=>false));
			foreach( $product_list as $product )
			{
				$total += $product->price * $product->count;
				$pr_total = $product->price * $product->count;

				$products.='
							<tr>
								<td><a href="'.$product->url.'" target="_blank">Midea '.$product->name.'</a></td>
								<td class="td_center">'.$product->{$order->user_price}.' руб.</td>
								<td class="td_center">'.$product->count.'</td>
								<td class="td_right">'.getPriceFormat( $pr_total ).' руб.</td>
							</tr>
							';
			}
			$adress = ( $order->delivery_type == 'courier' ) ? '<strong>Адрес доставки:</strong> '.$adresses[$order->delivery_adress] : '';
			$delivery_type = '<strong>Способ доставки:</strong> '.( $adress ? 'Курьер ('.getPriceFormat( $courier_price ).' руб.)' : 'Самовывоз' );
			$total += $courier_price;
			$orders .= '
					<tr class="tr_second">
						<td class="td_center"><span class="order_block">'.$order->id.'</span></td>
						<td class="td_center">'.$order->date_order_f.'</td>
						<td class="td_center">'.$statuses[$order->status].'</td>
						<td class="td_right">'.getPriceFormat($total).' руб.</td>
					</tr>
					<tr class="order_products" order_id="'.$order->id.'">
						<td colspan="4">
							<table class="product_list">
								<thead>
									<tr>
										<th class="td_left">Наименование товара</th>
										<th class="price">Цена</th>
										<th class="count">Кол-во</th>
										<th class="td_right">Итог. цена</th>
									</tr>
								</thead>
								<tbody>
									'.$products.'
									<tr>
										'.( $adress ? '<td colspan="2"><div class="adress">'.$adress.'</div></td>' : '' ).'
										<td colspan="'.( $adress ? '2' : '4' ).'"><div class="courier">'.$delivery_type.'</div></td>
									</tr>
								</tbody>
							</table>
						</td>
					</tr>
			';
		}

					
		$view->gText .= '
						<!-- table Start -->
						<table class="order_list">
							<thead>
								<tr>
									<td class="td_center">№ заказа</td>
									<td class="td_center big">Время заказа</td>
									<td class="td_center">Статус заказа</td>
									<td class="td_right">Стоимость</td>
								</tr>
							</thead>
							<tbody>
								'.$orders.'
							</tbody>	
						</table>
						<!-- table end -->
					';
	}
	else
		$view->gText .= '<h3>У Вас еще нет заказов</h3>';
	
